package teacher.service;

import java.util.Date;
import beanDao.MapToBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import database.dao.MysqlCrud;

import teacher.bean.Patent;
import teacher.bean.Teacher;
import teacher.bean.Paper;
import teacher.bean.Treatise;
import teacher.page.Page;
import tools.StringDate;

public class paper {
	
	/**
	 * @category 通过论著ID查询一项论文
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public Paper selectPaperById(int a) {
		Paper paperTwo = new Paper();
		String sql= "select * from paper where papId=?";
		int i=0;	
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(a)};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		if(1==list.size()){
			Map map=list.get(0);
			paperTwo= new MapToBean().MapToPaper(map);
		
		}
		return paperTwo;
	}
	
	/**
	 * @category 用于论文信息的修改
	 * @param treatise 修改的内容
	 * @param a 定位修改的位置
	 * @return 修改成功，返回true，否则返回flase
	 */
	public boolean updatePaper(Paper paper,int a) {
		String sql= "update paper set papId=?,name=?,fauthor=?,pbulication=?,pubNumber=?,include=?,quoteNum=?,state=?,upload=?,pubPage=?,date=?,topStu=?,tauthor=?,oauthor=?,volume=?,teaId=?,type=?,esi=?," +
				"jclass=?,includNum=?,meetClass=?,meetName=?,meetPlace=? where papId="+a+"";
		String[] parameter = {
				String.valueOf(paper.getPapId()),
				String.valueOf(paper.getName()),
				String.valueOf(paper.getFauthor()),
				String.valueOf(paper.getPbulication()),
				String.valueOf(paper.getPubNumber()),
				String.valueOf(paper.getInclude()),
				String.valueOf(paper.getQuoteNum()),
				String.valueOf(paper.getState()),
				String.valueOf(paper.getUpload()),
				String.valueOf(paper.getPubPage()),
				String.valueOf(paper.getDate()),
				String.valueOf(paper.getTopStu()),
				String.valueOf(paper.getTauthor()),
				String.valueOf(paper.getOauthor()),
				String.valueOf(paper.getVolume()),
				String.valueOf(paper.getTeaId()),
				String.valueOf(paper.getType()),
				String.valueOf(paper.getEsi()),
				String.valueOf(paper.getJclass()),
				String.valueOf(paper.getIncludNum()),
				String.valueOf(paper.getMeetClass()),
				String.valueOf(paper.getMeetName()),
				String.valueOf(paper.getMeetPlace())
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 统计一个老师一定时间论文的数量
	 * @return sum int 论著的数量
	 */
		public int getCountPaperTeaTime(Teacher tea,Date date1,Date date2)
		{
			int sum=0;
			String[] parameter={String.valueOf(tea.getTeaId())};
			String sql="select count(*) from paper where date>='"+date1+"' and date<='"+date2+"' and teaId=?";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists=mysqlcurd.SelectData(sql, parameter);
			Map map=lists.get(0);
			sum=Integer.parseInt(map.get(0).toString());
			return sum;
		}
		
		/**
		 * @category 统计一个老师按照模糊查询论文的数量
		 * @return sum int 论著的数量
		 */
			public int getCountPaperTeaVague(String vague,Teacher t)
			{
				int sum=0;
				String[] parameter={String.valueOf(t.getTeaId())};
				String sql="select count(*) from paper where name like '%"+vague+"%' and teaId=?";
				MysqlCrud mysqlcurd=new MysqlCrud();
				List<Map> lists=mysqlcurd.SelectData(sql, parameter);
				Map map=lists.get(0);
				sum=Integer.parseInt(map.get(0).toString());
				return sum;
			}
	
	/**
	 * @category 通过老师查询论文分页
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Paper> selectPaperByTeacherPage(Teacher t1,Page page) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where acheve.teaId=? limit ?,?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={t1.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 通过时间段查询老师论文分页
	 * @return paperOne List<Treatise> 返回查询到的专利列表
	 */
	public List<Paper> selectTimePaperTeaPage(Teacher t,Date date1,Date date2,Page page) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where acheve.date>='"+date1+"' and acheve.date<='"+date2+"' and acheve.teaId=? limit ?,?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={t.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 通过字段模糊查询老师论文分页
	 * @param vague 模糊查询需要字段
	 * @return treatiseOne List<Treatise> 返回查询到的论著列表
	 */
	public List<Paper> selectVaguePaperTeaPage(String vague,Teacher t1,Page page) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where acheve.name like '%"+vague+"%' and acheve.teaId=? limit ?,?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={ t1.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 通过时间段查询老师专利
	 * @return paperOne List<Paper> 返回查询到的专利列表
	 */
	public List<Paper> selectTimePaperTea(Teacher t,Date date1,Date date2) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where acheve.date>='"+date1+"' and acheve.date<='"+date2+"' and acheve.teaId=?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(t.getTeaId())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 按ID查询论文
	 * @param paper paper 传入论文的ID
	 * @return paper paper 返回一个论文对象
	 */
	public Paper SelectPaperById(Paper paper){		
		Paper paper1 = new Paper();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;
		if(null != String.valueOf(paper.getPapId())) {
			sql = "select * from paper where papId=?";
			String[] parameter={String.valueOf(paper.getPapId())};
			list = mysql.SelectData(sql, parameter);
		}
		if(1 == list.size()) {
			Map map=list.get(0);
			paper1=new MapToBean().MapToPaper(map);
		} 
		return paper1;
	}
	
	
	/**
	 * @category 通过老师查询论文
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Paper> selectPaperByTeacher(Teacher tea) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as t where acheve.teaId = t.teaId and t.user = ?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 查询所有论文
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Paper> selectAllPaper() {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where (acheve.state=4 or acheve.state=5)";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, null);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 查询老师第一作者不是学生的论文
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Paper> selectNotStu(Teacher tea) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as t where acheve.teaId = t.teaId and acheve.topStu=0 and t.user = ?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			paperOne.add(new MapToBean().MapToPaper(map));
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 查询老师第一作者是学生的论文
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Paper> selectYesStu(Teacher tea) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as t where acheve.teaId = t.teaId and acheve.topStu=1 and t.user = ?";
		int i=0;
		List<Paper> paperOne=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			try {
				Paper paperTwo = new Paper();
				paperTwo.setPapId(Integer.parseInt(map.get(0).toString()));
				paperTwo.setName(map.get(1).toString());
				paperTwo.setFauthor(map.get(2).toString());
				paperTwo.setPbulication(map.get(3).toString());
				paperTwo.setPubNumber(map.get(4).toString());
				paperTwo.setInclude(Integer.parseInt(map.get(5).toString()));
				paperTwo.setQuoteNum(Integer.parseInt(map.get(6).toString()));
				paperTwo.setState(Integer.parseInt(map.get(7).toString()));
				paperTwo.setUpload(map.get(8).toString());
				paperTwo.setPubPage(map.get(9).toString());
				paperTwo.setDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(10).toString()));
				paperTwo.setTopStu(Integer.parseInt(map.get(11).toString()));
				paperTwo.setTauthor(map.get(12).toString());
				paperTwo.setOauthor(map.get(13).toString());
				paperTwo.setVolume(map.get(14).toString());
				paperTwo.setTeaId(Integer.parseInt(map.get(15).toString()));
				paperTwo.setType(Integer.parseInt(map.get(16).toString()));
				paperTwo.setEsi(Integer.parseInt(map.get(17).toString()));
				paperTwo.setJclass(Integer.parseInt(map.get(18).toString()));
				paperTwo.setIncludNum(map.get(19).toString());
				paperTwo.setMeetClass(Integer.parseInt(map.get(20).toString()));
				paperTwo.setMeetName(map.get(21).toString());
				paperTwo.setMeetPlace(map.get(22).toString());
				paperOne.add(paperTwo);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			i++;
		}
		return paperOne;
	}
	
	/**
	 * @category 统计论文的数量
	 * @return sum int 论文的数量
	 */
		public int getCountPaper()
		{
			int sum=0;
			String sql="select count(*) from paper where state>3";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists=mysqlcurd.SelectData(sql, null);
			Map map=lists.get(0);
			sum=Integer.parseInt(map.get(0).toString());
			return sum;
		}
		
		/**
		 * @category 统计一个老师论文的数量
		 * @return sum int 论文的数量
		 */
			public int getCountPaperTea(Teacher tea)
			{
				int sum=0;
				String[] parameter={String.valueOf(tea.getTeaId())};
				String sql="select count(*) from paper where teaId=?";
				MysqlCrud mysqlcurd=new MysqlCrud();
				List<Map> lists=mysqlcurd.SelectData(sql, parameter);
				Map map=lists.get(0);
				sum=Integer.parseInt(map.get(0).toString());
				return sum;
			}
	
		/**
		 * @category 通过字段模糊查询论文
		 * @param vague 模糊查询需要字段
		 * @return paperOne List<Paper> 返回查询到的论文列表
		 */
		public List<Paper> selectVaguePaper(String vague) {
			String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where (acheve.state=4 or acheve.state=5) and acheve.name like '%"+vague+"%'";
			int i=0;
			List<Paper> paperOne=new ArrayList<Paper>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> list = mysqlcurd.SelectData(sql, null);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				paperOne.add(new MapToBean().MapToPaper(map));
				i++;
			}
			return paperOne;
		}
		
		/**
		 * @category 通过字段模糊查询老师论文
		 * @param vague 模糊查询需要字段
		 * @return paperOne List<Paper> 返回查询到的论文列表
		 */
		public List<Paper> selectVaguePaperTea(String vague,Teacher t) {
			String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where acheve.name like '%"+vague+"%' and acheve.teaId=?";
			int i=0;
			List<Paper> paperOne=new ArrayList<Paper>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			String[] parameter={String.valueOf(t.getTeaId())};
			List<Map> list = mysqlcurd.SelectData(sql, parameter);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				paperOne.add(new MapToBean().MapToPaper(map));
				i++;
			}
			return paperOne;
		}
		
		/**
		 * @category 通过时间段查询论文
		 * @return paperOne List<Paper> 返回查询到的论文列表
		 */
		public List<Paper> selectTimePaper(Date date1,Date date2) {
			String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve where (acheve.state=4 or acheve.state=5) and acheve.date>='"+date1+"' and acheve.date<='"+date2+"'";
			int i=0;
			List<Paper> paperOne=new ArrayList<Paper>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> list = mysqlcurd.SelectData(sql, null);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				paperOne.add(new MapToBean().MapToPaper(map));
				i++;
			}
			return paperOne;
		}
		
		/**
		 * @category 插入一条论文数据
		 * @param paper 所要插入的论文数据
		 * @return 插入成功返回true，否则返回false
		 */
		public boolean insertPaper(Paper paper){
			boolean flag = false;
			String sql="insert into paper(name,fAuthor,pbulication,pubNumber,include,quoteNum,state,upload,pubPage,date,topStu,tauthor,oauthor,volume,teaId,type,esi,jClass,includNum,meetClass,meetName,meetPlace)"+
			" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			String[] parameter={
					String.valueOf(paper.getName()),
					String.valueOf(paper.getFauthor()),
					String.valueOf(paper.getPbulication()),
					String.valueOf(paper.getPubNumber()),
					String.valueOf(paper.getInclude()),
					String.valueOf(paper.getQuoteNum()),
					String.valueOf(paper.getState()),
					String.valueOf(paper.getUpload()),
					String.valueOf(paper.getPubPage()),
					String.valueOf(paper.getDate()),
					String.valueOf(paper.getTopStu()),
					String.valueOf(paper.getTauthor()),
					String.valueOf(paper.getOauthor()),
					String.valueOf(paper.getVolume()),
					String.valueOf(paper.getTeaId()),
					String.valueOf(paper.getType()),
					String.valueOf(paper.getEsi()),
					String.valueOf(paper.getJclass()),
					String.valueOf(paper.getIncludNum()),
					String.valueOf(paper.getMeetClass()),
					String.valueOf(paper.getMeetName()),
					String.valueOf(paper.getMeetPlace())
			};
			MysqlCrud mysqlcurd=new MysqlCrud();
			flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
			return flag;
		}
		
		/**
		 * @category 更新一条论文数据
		 * @param paper1 改后
		 * @param paper2 改前
		 * @return 插入成功返回true，否则返回false
		 */
		public boolean updatePaper(Paper paper1,Paper paper2){
			boolean flag = false;
			String sql= "update paper set papId=?,name=?,fAuthor=?,pbulication=?,pubNumber=?,include=?,quoteNum=? ,state=?,upload=?,pubPage=?,topStu=?,tAuthor=?,oAuthor=?,volume=?,teaId=?,type=?,esi=?,jClass=?,includNum=?,meetClass=? where papId="+paper2.getPapId()+"";
			String[] parameter={
					String.valueOf(paper1.getPapId()),
					String.valueOf(paper1.getName()),
					String.valueOf(paper1.getFauthor()),
					String.valueOf(paper1.getPbulication()),
					String.valueOf(paper1.getPubNumber()),
					String.valueOf(paper1.getInclude()),
					String.valueOf(paper1.getQuoteNum()),
					String.valueOf(paper1.getState()),
					String.valueOf(paper1.getUpload()),
					String.valueOf(paper1.getPubPage()),
					/*String.valueOf(paper1.getDate()),*/
					String.valueOf(paper1.getTopStu()),
					String.valueOf(paper1.getTauthor()),
					String.valueOf(paper1.getOauthor()),
					String.valueOf(paper1.getVolume()),
					String.valueOf(paper1.getTeaId()),
					String.valueOf(paper1.getType()),
					String.valueOf(paper1.getEsi()),
					String.valueOf(paper1.getJclass()),
					String.valueOf(paper1.getIncludNum()),
					String.valueOf(paper1.getMeetClass()),
					String.valueOf(paper1.getMeetName()),
					String.valueOf(paper1.getMeetPlace())
			};
			MysqlCrud mysqlcurd=new MysqlCrud();
			flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
			return flag;
		}
		
		/**
		 * @category 用于删除论文的信息
		 * @return 删除成功返回true，否则为false
		 */
		public boolean DeletePaper(Paper paper){
			String sql = "delete from paper where papId=?";
			String[]  parameter={String.valueOf(paper.getPapId())};
			 MysqlCrud mysqlcurd=new MysqlCrud();
			 boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
			 return flag;
		}
		

}
